<?php

namespace StarCloud\Library\Sdk;

use AlibabaCloud\SDK\Alimt\V20181012\Alimt;
use AlibabaCloud\SDK\Alimt\V20181012\Models\GetDocTranslateTaskRequest;
use AlibabaCloud\SDK\Alimt\V20181012\Models\TranslateGeneralRequest;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Alimt\V20181012\Models\CreateDocTranslateTaskRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class TranslateBot {

    /**
     * 使用AK&SK初始化账号Client
     * @return Alimt Client
     */
    public static function createClient(){
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/311677.html。
        $config = new Config([
            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
            "accessKeyId" => 'LTAI5tAVLJdnByK5g1VZbHuy',
            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
            "accessKeySecret" => 'iyJJcUTBi76tadziwuxBJhy5LvpTi1'
        ]);

        // Endpoint 请参考 https://api.aliyun.com/product/alimt
        $config->endpoint = "mt.cn-hangzhou.aliyuncs.com";
        return new Alimt($config);
    }

    public static function textTranslate($text, $sourceLanguage='en', $targetLanguage = "zh", $formatType ="text", $scene = 'general'){
        $client = self::createClient();
        $translateGeneralRequest = new TranslateGeneralRequest([
            "formatType" => $formatType,
            "sourceLanguage" => $sourceLanguage,
            "targetLanguage" => $targetLanguage,
            "scene" => $scene,
            "sourceText" => $text
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            // 复制代码运行请自行打印 API 的返回值
            $result = $client->translateGeneralWithOptions($translateGeneralRequest, $runtime);
            return $result->body->data->translated;
        }catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            var_dump($error->message);
            // 诊断地址
            var_dump($error->data["Recommend"]);
            Utils::assertAsString($error->message);
        }
    }

    public static function queryDocTranslateTask($taskId)
    {
        $client = self::createClient();
        $getDocTranslateTaskRequest = new GetDocTranslateTaskRequest([
            "taskId" => $taskId,
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            // 复制代码运行请自行打印 API 的返回值
            $result = $client->getDocTranslateTaskWithOptions($getDocTranslateTaskRequest, $runtime);
            var_dump($result);
            return $result->body->translateFileUrl;
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            var_dump($error->message);
            // 诊断地址
            var_dump($error->data["Recommend"]);
            Utils::assertAsString($error->message);
        }
    }
    /**
     * @param string[] $args
     * @return void
     */
    public static function docTranslate($fileUrl, $sourceLanguage='en', $targetLanguage = "zh"){
        $client = self::createClient();
        $createDocTranslateTaskRequest = new CreateDocTranslateTaskRequest([
            "sourceLanguage" => $sourceLanguage,
            "targetLanguage" => $targetLanguage,
            "fileUrl" => $fileUrl
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            // 复制代码运行请自行打印 API 的返回值
            $result = $client->createDocTranslateTaskWithOptions($createDocTranslateTaskRequest, $runtime);
            var_dump($result);
            return $result->body->taskId;
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            var_dump($error->message);
            // 诊断地址
            var_dump($error->data["Recommend"]);
            Utils::assertAsString($error->message);
        }
    }
}

